<?php

namespace Data;

class PhotoComments extends Base {

	public function addComment($userId, $photoId, $content) {
		$data = array(
			'UserId' => $userId,
			'PhotoId' => $photoId,
			'Content' => $content,
			'CreateTime' => time(),
			'Status' => self::IS_YES,
		);
		$this->insert($data);
	}

	public function getCommentsByPhotoId($photoId, $pager) {
		$condition = array(
			'c.PhotoId' => $photoId,
			'c.Status' => self::IS_YES,
			'u.Status' => self::IS_YES,
		);
		$result = $this->select('c.*, u.Email, u.Username, u.Sex, u.Photo')
			->from('PhotoComments as c')
			->innerJoin('User as u', 'u.UserId = c.UserId')
			->where($condition)
			->order('CommentId desc')
			->pager($pager)
			->findAll();
		return $result;
	}

	public function getCountByIds($photoIds) {
		$condition = array(
			'PhotoId in' => $photoIds,
			'Status' => self::IS_YES,
		);
		$data = $this->select('count(1) as number, PhotoId')->where($condition)->group('PhotoId')->findAll();
		$result = array();
		foreach($data as $val) {
			$result[$val['PhotoId']] = $val['number'];
		}
		return $result;
	}
}